package com.hongyuan.coach.ui.person.activity;

import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.AppViewModelFactory;
import com.hongyuan.coach.base.CustomActivity;
import com.hongyuan.coach.databinding.ActivitySettingBinding;
import com.hongyuan.coach.ui.person.view_mode.SettingViewModel;
import com.hongyuan.coach.utils.CacheUtil;
import com.hongyuan.coach.utils.CustomDialog;

import me.tatarka.bindingcollectionadapter2.BR;

public class SettingActivity extends CustomActivity<ActivitySettingBinding, SettingViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_setting;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SettingViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SettingViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        binding.comBack.setOnClickListener(view -> finish());

        try {
            binding.cacheNum.setText(CacheUtil.getTotalCacheSize(this));
        }catch (Exception e){
            e.printStackTrace();
        }

        binding.cacheBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CacheUtil.clearAllCache(SettingActivity.this);
                try {
                    binding.cacheNum.setText(CacheUtil.getTotalCacheSize(SettingActivity.this));
                }catch (Exception e){
                    e.printStackTrace();
                }
                ToastUtils.showShort("缓存清除成功！");
            }
        });
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.outLogin.observe(this, aVoid -> {
            CustomDialog.promptDialog(this, "确定要退出登录吗？", "确定！", "不确定？", false, v1 -> {
                if(v1.getId() == R.id.isOk){
                    //退出登录
                    viewModel.outLogin();
                }
            });
        });
    }
}
